﻿#include "gitfunc.h"

int git_init()
{
	FILE *fp;
	char filename[50]="\0";
	//文件夹名称
    char folderName[40]=".git\0";
    // 文件夹不存在则创建文件夹
    if (_access(folderName, 0) == -1)
    {
        _mkdir(folderName);
    }
    strcpy(folderName,"./.git/objects\0");
    if (_access(folderName, 0) == -1)
    {
        _mkdir(folderName);
    }
    strcpy(folderName,"./.git/refs\0");
    if (_access(folderName, 0) == -1)
    {
        _mkdir(folderName);
    }
    strcpy(folderName,"./.git/refs/heads\0");
    if (_access(folderName, 0) == -1)
    {
        _mkdir(folderName);
    }
    strcpy(folderName,"./.git/refs/tags\0");
    if (_access(folderName, 0) == -1)
    {
        _mkdir(folderName);
    }
    strcpy(filename,"./.git/refs/heads/master.txt");
    fp=fopen(filename,"wt+");
    fclose(fp);
    strcpy(filename,"./.git/HEAD.txt");
   	fp=fopen(filename,"wt+");
   	fprintf(fp,"ref: refs/heads/master");
   	fclose(fp);
   	
}
//模拟diff功能比较以行为单位比较文档
int git_diff(char* txt1,char* txt2)
{
	FILE *fp,*fq;
	int len1,len2,l=0;
	char dstr1[MAX],dstr2[MAX],s1[40]="\0",s2[40]="\0";
	strcat(s1,txt1);strcat(s2,txt2);
	if((fp=fopen(s1,"r"))==NULL)  //以只读方式打开文件
	{
		printf("cannot open txt1\n");
		exit(1);
	}
 	if((fq=fopen(s2,"r"))==NULL)
	{
		printf("cannot open txt2\n");
		exit(1);
	}
	printf("git diff - %s + %s\n",txt1,txt2);
 	while(fgets(dstr1,MAX,fp)!= NULL && fgets(dstr2,MAX,fq)!= NULL) //逐行比较两个文档是否有差异
	{
 		//去掉换行符
 		len1=strlen(dstr1);
 		len2=strlen(dstr2);
 		if(dstr1[len1-1]=='\n' || dstr1[len1-1]=='\r')
		{
			dstr1[len1-1]='\0';
		}
 		if(dstr2[len2-1]=='\n' || dstr2[len2-1]=='\r')
		{
			dstr2[len2-1]='\0';
		}
 		//比较
		if(strcmp(dstr1,dstr2))
 		{
 			if(!strcmp(dstr1,"")) printf("-%d (none)\n+%d %s\n",l,l,dstr2);
 			else if(!strcmp(dstr2,"")) printf("-%d %s\n+%d (none)\n",l,dstr1,l);
			else printf("-%d %s\n+%d %s\n",l,dstr1,l,dstr2);
		}
		l++;
 	}
 	while(fgets(dstr1,MAX,fp)!= NULL) //文件1行数多于文件2行数
 	{
 		len1=strlen(dstr1);
 		if(dstr1[len1-1]=='\n' || dstr1[len1-1]=='\r')
		{
			dstr1[len1-1]='\0';
		}
		if(!strcmp(dstr1,"")) 
		{
			l++;
			continue;
		}
		printf("-%d %s\n+%d (none)\n",l,dstr1,l);
		l++;
	}
	while(fgets(dstr2,MAX,fq)!= NULL) //文件1行数少于文件2行数
 	{
 		len2=strlen(dstr2);
 		if(dstr2[len2-1]=='\n' || dstr1[len2-1]=='\r')
		{
			dstr2[len2-1]='\0';
		}
		if(!strcmp(dstr2,""))
		{
			l++;
			continue;
		}
		printf("-%d (none)\n+%d %s\n",l,l,dstr2);
		l++;
	}
	fclose(fp);
	fclose(fq);
 	return 0;
}

int git_add(char* txt1,char index[5][40])
{
	FILE *fp;
	int i,j;
	char dstr1[MAX]="blob \0",temp[20][MAX],output[50],s1[50]="\0";
	strcat(s1,txt1);
	if((fp=fopen(s1,"r"))==NULL)  //以只读方式打开文件
	{
		printf("cannot open txt1\n");
		exit(1);
	}
	for(i=0;fgets(temp[i],MAX,fp)!= NULL;i++)
	{
		
	}
	for(j=i-1;j>0;j--)
	{
		strcat(temp[j-1],temp[j]);
	}
	for(j=i;j<20;j++)
	{
		strcpy(temp[i],"\0");
	}
	strcpy(s1,itoa(strlen(temp[0]),s1,10));
	strcat(dstr1,s1);
	strcat(dstr1," ");
	strcat(dstr1,temp[0]);
	//puts(dstr1);
	sha1(dstr1,output);
	puts(output);
	for(i=0;i<20;i++)
	{
		if(index[i][0]=='\0') 
		{
			strcpy(index[i],txt1);
			break;
		}
	}
	//生成object
	strcut(0,1,output,s1);
	strcpy(dstr1,"./.git/objects/");
	strcat(dstr1,s1);
    if (_access(dstr1, 0) == -1)
    {
        _mkdir(dstr1);
    }
    strcut(2,39,output,s1);
    strcat(s1,".txt");
    strcat(dstr1,"/");
	strcat(dstr1,s1);
    fp=fopen(dstr1,"wt+");
    fprintf(fp,temp[0]);
    fclose(fp);
}

int git_status(char index[5][40])
{
	int i=0,j,k,m;
	 //文件存储信息结构体 
    struct _finddata_t fileinfo;
    //保存文件句柄 
    long fHandle;
    //文件数记录器
    char found[5][20]={"\0","\0","\0","\0","\0"};
    char untracked[5][20]={"\0","\0","\0","\0","\0"};
    if( (fHandle=_findfirst( "*.txt", &fileinfo )) == -1L ) 
    {
        printf( "cannot find any *.txt!\n");
        return 0;
    }
    else{
        do{
            strcpy(found[i++],fileinfo.name);
        }while( _findnext(fHandle,&fileinfo)==0);
    }
    //关闭文件 
    _findclose( fHandle );
    //暂存区内容输出
    printf("index:\n");
	for(i=0;i<5;i++)
	{
		puts(index[i]);
		if(index[i][0]=='\0') 
		{
			break;
		}
	}
	//将found数组中的字符串逐个与index区的字符串比较，若found中的某个字符串未在index中出现，则放入untracked数组中
	for(i=0,m=0;i<5;i++,k=0)
	{
		if(found[i][0]=='\0') 
		{
			break;
		}
		for(j=0,k=0;j<5;j++)
		{
			if(index[j][0]=='\0') 
			{
				break;
			}
			if(!strcmp(index[j],found[i]))
			{
				k++;
			}
		}
		if(!k)
		{
			strcpy(untracked[m++],found[i]);
		}
	}
	//untracked字符串数组输出
	printf("untracked:\n");
	for(i=0;i<5;i++)
	{
		puts(untracked[i]);
		if(untracked[i][0]=='\0') 
		{
			break;
		}
	}
}

int git_commit(char *option,char *message,char index[5][40])
{
	FILE *fp;
	int i,j;
	char dstr1[MAX]="tree \0",temp[20][MAX],output[50],s1[50]="\0";
	if(!strcmp(option,"-m"))
	{
		for(i=0;i<5 && index[i][0]!='0';i++)
		{
			strcat(s1,index[i]);
			if((fp=fopen(s1,"r"))==NULL)  //以只读方式打开文件
			{
				printf("cannot open txt1\n");
				exit(1);
			}
		}
		for(i=0;fgets(temp[i],MAX,fp)!= NULL;i++)
		{
		
		}
		for(j=i-1;j>0;j--)
		{
		strcat(temp[j-1],temp[j]);
		}
		for(j=i;j<20;j++)
		{
			strcpy(temp[i],"\0");
		}
		strcpy(s1,itoa(strlen(temp[0]),s1,10));
		strcat(dstr1,s1);
		strcat(dstr1," ");
		strcat(dstr1,temp[0]);
		strcat(dstr1,"\n");
		strcat(dstr1,message);
		//puts(dstr1);
		sha1(dstr1,output);
		//生成object
		strcut(0,1,output,s1);
		strcpy(dstr1,"./.git/objects/");
		strcat(dstr1,s1);
  	  	if (_access(dstr1, 0) == -1)
  	 	{
   	    	_mkdir(dstr1);
  		}
    	strcut(2,39,output,s1);
    	strcat(s1,".txt");
    	strcat(dstr1,"/");
		strcat(dstr1,s1);
    	fp=fopen(dstr1,"wt+");
    	fprintf(fp,temp[0]);
    	//清空index
    	for(i=0;i<5;i++) index[i][0]='0';
    	//修改master
    	fp=fopen("./.git/refs/heads/master.txt","wt+");
    	fprintf(fp,output);
    	fclose(fp);
	}
	else printf("unknown option!\n");
}